package com.xuyiwei.mayi;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by xuyiwei on 2019/4/17.
 */
@WebServlet("/DoFormServlet")
public class DoFormServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        boolean flag = isFlag(req);
        if (!flag) {
            resp.getWriter().write("已经提交...");
            System.out.println("数据已经提交了..");
            return;
        }
        String userName = req.getParameter("userName");
        try {
            Thread.sleep(300);
        } catch (Exception e) {
            // TODO: handle exception
        }
        System.out.println("往数据库插入数据...." + userName);
        resp.getWriter().write("success");
    }
    public boolean isFlag(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String sesionToken = (String) session.getAttribute("sesionToken");
        String token = request.getParameter("token");
        if (!(token.equals(sesionToken))) {
            return false;
        }
        session.removeAttribute("sesionToken");
        return true;
    }


}
